package com.webshop.servlet;

import com.webshop.Dao.BusinessDao;
import com.webshop.domain.Business;
import com.webshop.domain.Cart;
import com.webshop.domain.user.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/BusinessServlet")
public class BusinessServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Cart> carts = (List<Cart>) req.getSession().getAttribute("carts");
        User user = (User) req.getSession().getAttribute("user");
        if (user==null){
            resp.sendRedirect("nologin.jsp");
            return;
        }
        Business business = new Business(user.getUsername());
        business.addCart(carts);
        req.getSession().removeAttribute("carts");
        BusinessDao.addBusiness(business);
        resp.sendRedirect("index.jsp");
    }
}
